import Vue from 'vue'
import VueRouter from 'vue-router'

//重写VueRouter.prototype.push|replace方法
//主要原因:引入的Promise抛出异常,原方法返回Promise函数
//保存原来的方法,留用,以保存原有的路由跳转功能

let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

VueRouter.prototype.push = function(location,resolve,reject){
    //判断程序员是否写入成功与失败的回调,若写入,则直接使用;若没写,直接使用定好的
    if(resolve && reject){
        //使用call方法改变其中this指向,使其指向调用push方法的实例
        originPush.call(this,location,resolve,reject)
    }else{
        originPush.call(this,location,() => {},() => {})
    }
}

VueRouter.prototype.replace = function(location,resolve,reject){
    //判断程序员是否写入成功与失败的回调,若写入,则直接使用;若没写,直接使用定好的
    if(resolve && reject){
        //使用call方法改变其中this指向,使其指向调用push方法的实例
        originReplace.call(this,location,resolve,reject)
    }else{
        originReplace.call(this,location,() => {},() => {})
    }
}

import Home from '@/pages/Home/index.vue'
import Login from '@/pages/Login/index.vue'
import Register from '@/pages/Register/index.vue'
import Search from '@/pages/Search/index.vue'
import Detail from '@/pages/Detail/index.vue'
import AddCartSuccess from '@/pages/AddCartSuccess/index.vue'
import ShopCart from '@/pages/ShopCart/index.vue'
Vue.use(VueRouter)

export default new VueRouter({
    routes:[
        {
            name:'home',
            path:'/home',
            component:Home,
            // meta属性,现阶段用于添加标记属性,条件渲染时可用
            meta:{show:true}
        },
        {
            path:'/login',
            component:Login,
            meta:{show:false}
        },
        {
            name:'search',
            path:'/search/:keyword?',
            component:Search,
            meta:{show:true},
            //传递参数
            props:(route)=>({...route.query})
        },
        {
            path:'/register',
            component:Register,
            meta:{show:false}
        },
        {
            name:'detail',
            path:'/detail/:skuId',
            component:Detail,
            meta:{show:true}
        },
        {
            name:'addcartsuccess',
            path:'/addcartsuccess',
            component:AddCartSuccess,
            meta:{show:true}
        },
        {
            name:'shopcart',
            path:'/shopcart',
            component:ShopCart,
            meta:{show:true}

        },
        {
            path:'/',
            redirect:'/home'
        }
    ]
})